/* This file was generated with JastAdd2 (http://jastadd.org) version R20121112 (r872) */
package parser;

import semanticlib.SymbolTable;
/**
 * @production ASTNode;
 * @ast node
 * 
 */
public class ASTNode<T extends ASTNode> extends SimpleNode  implements Cloneable, Iterable<T> {
  /**
   * @apilevel low-level
   */
  public void flushCache() {
  }
  /**
   * @apilevel internal
   */
  public void flushCollectionCache() {
  }
  /**
   * @apilevel internal
   */
  @SuppressWarnings({"unchecked", "cast"})
  public ASTNode<T> clone() throws CloneNotSupportedException {
    ASTNode node = (ASTNode)super.clone();
    return node;
  }
  /**
   * @apilevel internal
   */
  @SuppressWarnings({"unchecked", "cast"})
  public ASTNode<T> copy() {
      try {
        ASTNode node = (ASTNode)clone();
        if(children != null) node.children = (ASTNode[])children.clone();
        return node;
      } catch (CloneNotSupportedException e) {
      }
      System.err.println("Error: Could not clone node of type " + getClass().getName() + "!");
      return null;
  }
  /**
   * Create a deep copy of the AST subtree at this node.
   * The copy is dangling, i.e. has no parent.
   * @return dangling copy of the subtree at this node
   * @apilevel low-level
   */
  @SuppressWarnings({"unchecked", "cast"})
  public ASTNode<T> fullCopy() {
    try {
      ASTNode tree = (ASTNode) clone();
      tree.setParent(null);// make dangling
      if (children != null) {
        tree.children = new ASTNode[children.length];
        for (int i = 0; i < children.length; ++i) {
          if (children[i] == null) {
            tree.children[i] = null;
          } else {
            tree.children[i] = ((ASTNode) children[i]).fullCopy();
            ((ASTNode) tree.children[i]).setParent(tree);
          }
        }
      }
      return tree;
    } catch (CloneNotSupportedException e) {
      throw new Error("Error: clone not supported for " +
        getClass().getName());
    }
  }
  /**
   * @ast method 
   * @aspect NameAnalysis
   * @declaredat /h/d1/l/int13mka/workspace/CalcAnalysis/specification/NameAnalysis.jadd:4
   */
  void analyze(SymbolTable table) {
		for (int k=0; k<getNumChild(); k++) {
			getChild(k).analyze(table);
		}
	}
  /**
   * @ast method 
   * @aspect NameAnalysis
   * @declaredat /h/d1/l/int13mka/workspace/CalcAnalysis/specification/NameAnalysis.jadd:10
   */
  void addDecl(SymbolTable table) {
		for (int k=0; k<getNumChild(); k++) {
		getChild(k).addDecl(table);
		}
	}
  /**
   * @ast method 
   * @aspect DumpTreeWithSourceLines
   * @declaredat /h/d1/l/int13mka/workspace/CalcAnalysis/specification/PrintBindings.jadd:4
   */
  public String simpleClassName() {
	  String name = getClass().getName();
	  String[] splitname = name.split("[.]");
	  return splitname[splitname.length-1];
  }
  /**
   * @ast method 
   * @aspect DumpTreeWithSourceLines
   * @declaredat /h/d1/l/int13mka/workspace/CalcAnalysis/specification/PrintBindings.jadd:10
   */
  public void printBindings(String indent, java.io.PrintStream pStream) {
	    pStream.println(indent + simpleClassName() + tokenValue());
	    String childIndent = indent + "  ";
	    for(int i = 0; i < getNumChild(); i++)
	      getChild(i).printBindings(childIndent, pStream);
	  }
  /**
   * @ast method 
   * @aspect DumpTreeWithSourceLines
   * @declaredat /h/d1/l/int13mka/workspace/CalcAnalysis/specification/PrintBindings.jadd:17
   */
  public String tokenValue() { return ""; }
  /**
   * @ast method 
   * @aspect SourceLine
   * @declaredat /h/d1/l/int13mka/workspace/CalcAnalysis/specification/SourceLine.jadd:3
   */
  

  protected Token first;
  /**
   * @ast method 
   * @aspect SourceLine
   * @declaredat /h/d1/l/int13mka/workspace/CalcAnalysis/specification/SourceLine.jadd:4
   */
  
  protected Token last;
  /**
   * @ast method 
   * @aspect SourceLine
   * @declaredat /h/d1/l/int13mka/workspace/CalcAnalysis/specification/SourceLine.jadd:6
   */
  public String startLineCol() {
    return "(Line:" + first.beginLine + " Col:" + first.beginColumn + ")";
  }
  /**
   * @ast method 
   * @aspect SourceLine
   * @declaredat /h/d1/l/int13mka/workspace/CalcAnalysis/specification/SourceLine.jadd:10
   */
  public String endLineCol() {
    return "(Line:" + last.endLine + " Col:" + last.endColumn + ")";
  }
  /**
   * @ast method 
   * 
   */
  public ASTNode(int i) {
    super(i);
  }
  /**
   * @ast method 
   * 
   */
  public ASTNode(MiniP p, int i) {
    this(i);
    parser = p;
  }
  /**
   * @ast method 
   * 
   */
  public ASTNode() {
    this(0);


  }
  /**
   * @ast method 
   * 
   */
  public void dumpTree(String indent, java.io.PrintStream pStream) {
    pStream.println(indent + "ASTNode");
        String childIndent = indent + "  ";
    for(int i = 0; i < getNumChild(); i++)
      getChild(i).dumpTree(childIndent, pStream);
  }
  /**
   * @ast method 
   * 
   */
  public Object jjtAccept(MiniPVisitor visitor, Object data) {
    return visitor.visit(this, data);
  }
  /**
   * @ast method 
   * 
   */
  public void jjtAddChild(Node n, int i) {
    checkChild(n, i);
    if(i >= numChildren) numChildren = i+1;
    super.jjtAddChild(n, i);
}
  /**
   * @ast method 
   * 
   */
  public void checkChild(Node n, int i) {
  }
  /**
   * @apilevel internal
   * @ast method 
   * 
   */
  

  /**
   * @apilevel internal
   */
  public static final boolean generatedWithCircularEnabled = true;
  /**
   * @apilevel internal
   * @ast method 
   * 
   */
  
  /**
   * @apilevel internal
   */
  public static final boolean generatedWithCacheCycle = true;
  /**
   * @apilevel internal
   * @ast method 
   * 
   */
  
  /**
   * @apilevel internal
   */
  public static final boolean generatedWithComponentCheck = false;
  /**
   * @apilevel internal
   * @ast method 
   * 
   */
  
  /**
   * @apilevel internal
   */
  protected static ASTNode$State state = new ASTNode$State();
  /**
   * @apilevel internal
   * @ast method 
   * 
   */
  public final ASTNode$State state() { return state; }
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  @SuppressWarnings("cast")
  public T getChild(int i) {
    return (T)getChildNoTransform(i);
  }
  /**
   * @apilevel internal
   * @ast method 
   * 
   */
  
  /**
   * @apilevel internal
   */
  private int childIndex;
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  public int getIndexOfChild(ASTNode node) {
    if(node != null && node.childIndex < getNumChildNoTransform() && node == getChildNoTransform(node.childIndex))
      return node.childIndex;
    for(int i = 0; i < getNumChildNoTransform(); i++)
      if(getChildNoTransform(i) == node) {
        node.childIndex = i;
        return i;
      }
    return -1;
  }
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  public void addChild(T node) {
    setChild(node, getNumChildNoTransform());
  }
  /**
   * <p><em>This method does not invoke AST transformations.</em></p>
   * @apilevel low-level
   * @ast method 
   * 
   */
  @SuppressWarnings("cast")
  public final T getChildNoTransform(int i) {
    return (T) (children != null ? children[i] : null);
  }
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  
  /**
   * @apilevel low-level
   */
  protected int numChildren;
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  protected int numChildren() {
    return numChildren;
  }
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  public int getNumChild() {
    return numChildren();
  }
  /**
   * <p><em>This method does not invoke AST transformations.</em></p>
   * @apilevel low-level
   * @ast method 
   * 
   */
  public final int getNumChildNoTransform() {
    return numChildren();
  }
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  public void setChild(ASTNode node, int i) {
    if(children == null) {
      children = new ASTNode[i + 1];
    } else if (i >= children.length) {
      ASTNode c[] = new ASTNode[i << 1];
      System.arraycopy(children, 0, c, 0, children.length);
      children = c;
    }
    children[i] = node;
    if(i >= numChildren) numChildren = i+1;
    if(node != null) { node.setParent(this); node.childIndex = i; }
  }
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  public void insertChild(ASTNode node, int i) {
    if(children == null) {
      children = new ASTNode[i + 1];
      children[i] = node;
    } else {
      ASTNode c[] = new ASTNode[children.length + 1];
      System.arraycopy(children, 0, c, 0, i);
      c[i] = node;
      if(i < children.length) {
        System.arraycopy(children, i, c, i+1, children.length-i);
        for(int j = i+1; j < c.length; ++j) {
          if(c[j] != null)
            c[j].childIndex = j;
        }
      }
      children = c;
    }
    numChildren++;
    if(node != null) { node.setParent(this); node.childIndex = i; }
  }
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  public void removeChild(int i) {
    if(children != null) {
      ASTNode child = (ASTNode)children[i];
      if(child != null) {
        child.setParent(null);
        child.childIndex = -1;
      }
      System.arraycopy(children, i+1, children, i, children.length-i-1);
      numChildren--;
      for(int j = i; j < numChildren; ++j) {
        if(children[j] != null) {
          child = (ASTNode) children[j];
          child.childIndex = j;
        }
      }
    }
  }
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  public ASTNode getParent() {
    return (ASTNode)parent;
  }
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  public void setParent(ASTNode node) {
    parent = node;
  }
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  public java.util.Iterator<T> iterator() {
    return new java.util.Iterator<T>() {
      private int counter = 0;
      public boolean hasNext() {
        return counter < getNumChild();
      }
      @SuppressWarnings("unchecked") public T next() {
        if(hasNext())
          return (T)getChild(counter++);
        else
          return null;
      }
      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
  }
}
